package com.atguigu.lease.admin.controller.system;

import com.atguigu.lease.admin.service.SystemPostService;
import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.bean.SystemPost;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "后台用户岗位管理")
@RestController
@RequestMapping("/admin/system/post")
public class SystemPostController {

    @Autowired
    SystemPostService systemPostService;

    @Operation(summary = "1,分页查询岗位信息")
    @GetMapping("/page")
    public Result<Page<SystemPost>> page(
            @RequestParam Long current,
            @RequestParam Long size
    ) {
        Page<SystemPost> page = systemPostService.page(new Page<SystemPost>(current, size));
        //前端接口的字段与后端不一致
        page.getRecords().forEach(systemPost -> systemPost.setPostCode(systemPost.getCode()));
        return Result.ok(page);
    }

    @Operation(summary = "2,保存或更新岗位")
    @PostMapping("/saveOrUpdate")
    public Result saveOrUpdate(@RequestBody SystemPost systemPost) {
        //前端接口的字段与后端不一致
        systemPost.setCode(systemPost.getPostCode());
        return Result.ok(systemPostService.saveOrUpdate(systemPost));
    }

    @Operation(summary = "3,根据id删除岗位")
    @DeleteMapping("/deleteById")
    public Result deleteById(@RequestParam Long id) {
        return Result.ok(systemPostService.removeById(id));
    }

    @Operation(summary = "4,获取全部岗位列表")
    @GetMapping("/list")
    public Result<List<SystemPost>> list() {
        return Result.ok(systemPostService.list());
    }

    @Operation(summary = "5,根据id查询岗位")
    @GetMapping("/getById")
    public Result getById(@RequestParam Long id) {
        return Result.ok(systemPostService.getById(id));
    }

    @Operation(summary = "6,更新岗位发布状态")
    @PostMapping("/updateStatusByPostId")
    public Result updateStatusByPostId(@RequestParam Long id,
                                       @RequestParam String status) {
        systemPostService.update(Wrappers.lambdaUpdate(SystemPost.class)
                .eq(SystemPost::getId, id)
                .set(SystemPost::getStatus, status));
        return Result.ok();
    }
}
